package com.qidi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidi.bean.Employee;
import com.qidi.bean.Msg;
import com.qidi.service.EmployeeService;
import com.sun.org.apache.regexp.internal.RE;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import sun.java2d.pipe.SpanShapeRenderer;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author zhouXinBin
 * @create 2020-10-26-12:01
 */
@Controller
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;

    //模糊查询
    @RequestMapping("/empSearch")
    @ResponseBody
    public Msg getRelative(@RequestParam(value = "pn",defaultValue = "1")Integer pn,@RequestParam("content")String empName){
        PageHelper.startPage(pn,10);
        List<Employee> list=employeeService.getEmpsByName(empName);
        PageInfo<Employee> pageInfo=new PageInfo<>(list,5);
        return Msg.success().add("pageInfo",pageInfo);
    }


    //单次删除和批量删除二合一
    @RequestMapping(value = "/emp/{empId}",method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deletById(@PathVariable("empId")String ids){
        System.out.println("1:"+ids);
        if (ids.contains("-")){
            List<Integer> listId=new ArrayList<>();
            String[] batchIds = ids.split("-");
            System.out.println(Arrays.toString(batchIds));
            for (String batchId : batchIds) {
                listId.add(Integer.parseInt(batchId));
            }
            System.out.println("list:"+listId);
            employeeService.deleteBatchEmps(listId);

        }else {
            int id = Integer.parseInt(ids);
            employeeService.deletEmpById(id);
        }
        return Msg.success();
    }

    @RequestMapping(value = "/emp/{empId}",method = RequestMethod.PUT)
    @ResponseBody
    public Msg updateEmpById(Employee employee){
        employeeService.updateEmp(employee);
        return Msg.success();
    }


    @RequestMapping(value = "/emp/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Msg getEmpById(@PathVariable("id")Integer empId){
        Employee employee=employeeService.getEmpById(empId);
        return Msg.success().add("emp",employee);
    }

    @RequestMapping(value = "/checkname")
    @ResponseBody
    public Msg checkEmpName(@RequestParam("empName") String empName){
        //        先判断用户名是否是合法的表达式，再判断是否重复
        String regx = "(^[a-zA-Z0-9_-]{6,16}$)|(^[\\u2E80-\\u9FFF]{2,5})";
        if (!empName.matches(regx)){
            return Msg.fail().add("va_msg","用户名必须是6-16位数字，字母或者_-，也可以是2-5位中文组成");
        }else {
            //数据库用户名重复校验
            boolean b=employeeService.checkEmpName(empName);
            if (b){
                return Msg.success();
            }else {
                return Msg.fail().add("va_msg","用户名已存在");
            }
        }
    }
/**
 * 保存员工
 * 要支持JSR303，需要导入Hibernate-Validator包
 *
 * @Valid :封装对象之后，对数据进行校验
 * BindingResult :封装校验的结果
 */
    @RequestMapping(value = "/emp",method = RequestMethod.POST)
    @ResponseBody
    public Msg saveEmp(@Valid Employee employee, BindingResult result){
        if (result.hasErrors()){
            //            校验失败，在模态框中显示校验失败的错误信息
            Map<String,Object> map=new HashMap<>();
            List<FieldError> fieldErrors = result.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
                System.out.println("错误的字段名:"+fieldError.getField());
                System.out.println("错误信息:"+fieldError.getDefaultMessage());
                map.put(fieldError.getField(),fieldError.getDefaultMessage());
            }
            return Msg.fail().add("errorField",map);
        }else {
            employeeService.saveEmp(employee);
            return Msg.success();
        }
    }

    @RequestMapping("/emps")
    @ResponseBody
    public Msg getEmpsWithJson(@RequestParam(value = "pn",defaultValue = "1")Integer pn){
        PageHelper.startPage(pn,10);
       List<Employee> allEmp = employeeService.getAllEmp();
       PageInfo<Employee> pageInfo=new PageInfo<>(allEmp,5);

       return Msg.success().add("pageInfo",pageInfo);
    }

//    @RequestMapping("/emps" )
//    public String getEmps(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Map<String,Object> map){
//        PageHelper.startPage(pn,5);
//        List<Employee> allEmp = employeeService.getAllEmp();
//        PageInfo<Employee> pageInfo=new PageInfo<>(allEmp,5);
//
//        map.put("pageInfo",pageInfo);
//        return "list";
//    }
}
